package web;/**
 * @author 王俊彪
 * @version 1.0
 * 专门向前端响应用户信息
 */

import beans.LoginStatus;
import beans.User;
import utils.ResultUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/user/getUser")
public class GetUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User existUser =(User) session.getAttribute("existUser");
        //通过existUser的状态来确定用户有没有登录
        String responseData;
        if(existUser!=null){
            //说明登录成功
            responseData = ResultUtils.result(LoginStatus.LOGIN_SUCCESS,existUser);
        }else{
            //说明用户未登录 ，或者登出
            responseData = ResultUtils.result(LoginStatus.LOGIN_OUT,null);
        }
        response.getWriter().write(responseData);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
